package io.unicorn.modules.water.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.unicorn.modules.water.dao.TAreaInfoDao;
import io.unicorn.modules.water.entity.TAccountManageEntity;
import io.unicorn.modules.water.entity.TAreaInfoEntity;
import io.unicorn.modules.water.service.TAreaInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.unicorn.common.utils.PageUtils;
import io.unicorn.common.utils.Query;

import io.unicorn.modules.water.dao.TUniinfoWDao;
import io.unicorn.modules.water.entity.TUniinfoWEntity;
import io.unicorn.modules.water.service.TUniinfoWService;


@Service("tUniinfoWService")
public class TUniinfoWServiceImpl extends ServiceImpl<TUniinfoWDao, TUniinfoWEntity> implements TUniinfoWService {

    @Autowired
    private TAreaInfoService areaInfoService;
    @Autowired
    private TAreaInfoDao tAreaInfoDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String qAreaId = (String)params.get("qareaid");

        IPage<TUniinfoWEntity> page = this.page(
                new Query<TUniinfoWEntity>().getPage(params),
                new QueryWrapper<TUniinfoWEntity>()
                        .eq(StringUtils.isNotBlank(qAreaId),"area_id", qAreaId)
                        .ne("del_flag",1)
        );

        Set<String> areaIds = page.getRecords().stream().map(TUniinfoWEntity::getAreaId).collect(Collectors.toSet());
        List<TAreaInfoEntity> areaList = tAreaInfoDao.selectList(Wrappers.lambdaQuery(TAreaInfoEntity.class).in(TAreaInfoEntity::getAreaId, areaIds));
        Map<String, String> areaMap = areaList.stream().collect(Collectors.toMap(TAreaInfoEntity::getAreaId, TAreaInfoEntity::getAreaName));
        page.getRecords().forEach(e -> e.setAreaName(areaMap.get(e.getAreaId())));

        return new PageUtils(page);
    }

}
